package com.axxy.status;

/**
 * @author 一缕茜阳
 * @date 2021/8/28
 */
public class StatusModel {
    public static void main(String[] args) {
        StatusContext account = new StatusContext();
        System.out.println("学生成绩状态测试：");
        account.add(30);
        account.add(40);
        account.add(25);
        account.add(-15);
        account.add(-25);
    }
}

abstract class Status {
    protected StatusContext context;
    protected String name;
    protected int score;

    void addScore(int x) {
        score += x;
        System.out.println("加上：" + x + "分，当前分数：" + score );
        checkStatus();
        System.out.println("分，当前状态：" + context.getStatus().name);
    };

    abstract void checkStatus();

}

class StatusContext {
    private Status status;

    StatusContext() {
        status = new LowStatus(this);
    }

    public StatusContext(Status status) {
        this.status = new LowStatus(this);
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public void add(int score) {
        status.addScore(score);
    }

}

class LowStatus extends Status {

    public LowStatus(Status status) {
        context = status.context;
        name = "不及格";
        score = status.score;
    }

    public LowStatus(StatusContext statusContext) {
        context = statusContext;
        name = "不及格";
        score = 0;
    }

    @Override
    public void checkStatus() {
        if (score >= 90) {
            context.setStatus(new HighStatus(this));
        } else if (score >= 60) {
            context.setStatus(new MidStatus(this));
        }
    }
}

class MidStatus extends Status {

    public MidStatus(Status status) {
        context = status.context;
        name = "中等";
        score = status.score;
    }
    @Override
    void checkStatus() {
        if (score < 60) {
            context.setStatus(new LowStatus(this));
        } else if (score >= 90) {
            context.setStatus(new HighStatus(this));
        }
    }
}

class HighStatus extends Status {

    public HighStatus(Status status) {
        context = status.context;
        name = "优秀";
        score = status.score;
    }

    @Override
    void checkStatus() {
        if (score < 60) {
            context.setStatus(new LowStatus(this));
        } else if (score < 90) {
            context.setStatus(new MidStatus(this));
        }
    }
}